package com.community.backend.service.impl;

import com.alibaba.fastjson.JSON;
import com.community.backend.mapper.FeeMapper;
import com.community.backend.mapper.OrderMapper;
import com.community.backend.pojo.Fee;
import com.community.backend.pojo.dto.FeeQueryDTO;
import com.community.backend.pojo.vo.FeeQueryVO;
import com.community.backend.service.FeeService;
import com.community.backend.utils.PageResult;
import com.community.backend.utils.RedisUtils;
import com.community.backend.websocket.WebSocketServer;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class FeeServiceImpl implements FeeService {
    private final FeeMapper feeMapper;
    private final OrderMapper orderMapper;

    private final WebSocketServer webSocketServer;

    public FeeServiceImpl(FeeMapper feeMapper, OrderMapper orderMapper, WebSocketServer webSocketServer) {
        this.feeMapper = feeMapper;
        this.orderMapper = orderMapper;
        this.webSocketServer = webSocketServer;
    }
    @Override
    public PageResult pageQueryParking(FeeQueryDTO feeQueryDTO) {
        feeQueryDTO.setProject("停车费");
        PageHelper.startPage(feeQueryDTO.getCurrentPage(),feeQueryDTO.getPageSize());
        Page<FeeQueryVO> page = feeMapper.pageQuery(feeQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Override
    public PageResult pageQueryWater(FeeQueryDTO feeQueryDTO) {
        feeQueryDTO.setProject("水费");
        PageHelper.startPage(feeQueryDTO.getCurrentPage(),feeQueryDTO.getPageSize());
        Page<FeeQueryVO> page = feeMapper.pageQuery(feeQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Override
    public PageResult pageQueryPower(FeeQueryDTO feeQueryDTO) {
        feeQueryDTO.setProject("电费");
        PageHelper.startPage(feeQueryDTO.getCurrentPage(),feeQueryDTO.getPageSize());
        Page<FeeQueryVO> page = feeMapper.pageQuery(feeQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Override
    public PageResult pageQueryGas(FeeQueryDTO feeQueryDTO) {
        feeQueryDTO.setProject("燃气费");
        PageHelper.startPage(feeQueryDTO.getCurrentPage(),feeQueryDTO.getPageSize());
        Page<FeeQueryVO> page = feeMapper.pageQuery(feeQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void selectById(String id) {
        Fee fee = feeMapper.selectById(id);
        String userId = String.valueOf(fee.getUserId());
        System.out.println("fee"+fee);
        Map<String,Object> map = new HashMap<>();
        map.put("sid",userId);
        map.put("message", "费用类型：" + fee.getProject()); // 桌面通知 body
        map.put("feeId", fee.getId());            // 扩展字段，前端可跳转详情
        map.put("status", fee.getStatus());       // 已缴费/未缴费
        map.put("amount", fee.getTotalPrice());   // 金额
        String json = JSON.toJSONString(Map.of(
                "sid", userId,
                "message", "您有新的" + fee.getProject() + "账单,账单号："+fee.getId() +"金额：" + fee.getTotalPrice() + "元",
                "feeId", fee.getId()));
        String key = "offline:" + userId;          // 不再拼 feeId
        System.out.println("key:"+key);
        System.out.println("json:"+json);
        RedisUtils.redis().opsForList().rightPush(key, json);

//        redisTemplate.opsForValue().set("notification_fee_" + userId, map);
        // 推送
//        WebSocketServer.sendToOne(String.valueOf(fee.getUserId()),
//                JSON.toJSONString(map));
//        System.out.println(">>> WebSocket 推送完成，目标用户=" + fee.getUserId());
    }



    @Override
    @Transactional
    public void save(Fee fee) {
        fee.setId(UUID.randomUUID().toString());
        fee.setStatus("未缴");
        fee.setIsDelete("未删除");
        feeMapper.insert(fee);
    }

    @Override
    public void update(Fee fee) {
        feeMapper.update(fee);
    }

    @Override
    public void deleteById(String id) {
        Fee fee = new Fee();
        fee.setId(id);
        feeMapper.updateById(fee);
    }
}
